package com.uchengwang.ui.base_ui.ui;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.aliyun.common.httpfinal.QupaiHttpFinal;
import com.aliyun.demo.crop.AliyunVideoCrop;
import com.aliyun.demo.crop.MediaActivity;
import com.aliyun.demo.recorder.AliyunVideoRecorder;
import com.uchengwang.R;
import com.uchengwang.ui.cm_ui.release.bean.AliyunSTSTokenBean;
import com.uchengwang.ui.cm_ui.release.utils.VideoManagerUtils;
import com.uchengwang.ui.utils.ImageManagerUtils;
import com.uchengwang.ui.utils.UCommonUtil;

/**
 * Created by U城网 on 2018/3/16.
 */

public class TestVideoActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private String[] eff_dirs= new String[]{};
    public int REQUEST_RECORD = 0, REQUEST_CROP =2;
    private Button record, crop, upload;
    private String videoPath;
    private AliyunSTSTokenBean stsTokenBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test_video);

        record = (Button) findViewById(R.id.record);
        record.setOnClickListener(this);
        crop = (Button) findViewById(R.id.crop);
        crop.setOnClickListener(this);
        upload = (Button) findViewById(R.id.upload);
        upload.setOnClickListener(this);

        System.loadLibrary("live-openh264");
        System.loadLibrary("QuCore-ThirdParty");
        System.loadLibrary("QuCore");

        QupaiHttpFinal.getInstance().initOkHttpFinal();
//        com.aliyun.vod.common.httpfinal.QupaiHttpFinal.getInstance().initOkHttpFinal();

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, 1);
    }

    private void record(){
        //录制
        VideoManagerUtils.getUtils().aliyunRecord(this, REQUEST_RECORD);
    }

    private void crop(){
        //裁剪
//        VideoManagerUtils.getUtils().aliyunCrop(this, REQUEST_CROP);
        startActivity(new Intent(this, AliyunVideoCrop.class));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode ==  REQUEST_RECORD){
            if(resultCode == Activity.RESULT_OK && data!= null){
                int type = data.getIntExtra(AliyunVideoRecorder.RESULT_TYPE,0);
                if(type ==  AliyunVideoRecorder.RESULT_TYPE_CROP){
//                    String path = data.getStringExtra(AliyunVideoCrop.RESULT_KEY_CROP_PATH);//output_path
//                    Toast.makeText(this,"文件路径为 "+ path + " 时长为 " +
//                            data.getLongExtra(AliyunVideoCrop.RESULT_KEY_DURATION,0),Toast.LENGTH_SHORT).show();

                }else if(type ==  AliyunVideoRecorder.RESULT_TYPE_RECORD){
                    videoPath = data.getStringExtra(AliyunVideoRecorder.OUTPUT_PATH);
                    Toast.makeText(this,"文件路径为 "+
                            data.getStringExtra(AliyunVideoRecorder.OUTPUT_PATH),Toast.LENGTH_SHORT).show();

                }
            }else if(resultCode == Activity.RESULT_CANCELED){
                Toast.makeText(this,"用户取消录制",Toast.LENGTH_SHORT).show();
            }
        }

        if(requestCode == REQUEST_CROP){
            if(resultCode == Activity.RESULT_OK && data!= null){
                int type = data.getIntExtra(MediaActivity.RESULT_TYPE,0);
                if(type ==  MediaActivity.RESULT_TYPE_CROP){
//                    String path = data.getStringExtra(AliyunVideoCrop.RESULT_KEY_CROP_PATH);
//                    Toast.makeText(this,"文件路径为 "+ path + " 时长为 " +
//                            data.getLongExtra(AliyunVideoCrop.RESULT_KEY_DURATION,0),Toast.LENGTH_SHORT).show();
                }else if(type ==  MediaActivity.RESULT_TYPE_RECORD){
                    Toast.makeText(this,"文件路径为 "+
                            data.getStringExtra(AliyunVideoRecorder.OUTPUT_PATH),Toast.LENGTH_SHORT).show();
                }
            }else if(resultCode == Activity.RESULT_CANCELED){
                Toast.makeText(this,"用户取消裁剪",Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1){
            if (grantResults != null){
                UCommonUtil.getUtil().showToast(this, "权限已授予");
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.record:
                record();
                break;
            case R.id.crop:
//                crop();
//                VideoManagerUtils.getUtils().getVideoSTS(this, null, new OnHttpResultSingleListener<AliyunSTSTokenBean>() {
//                    @Override
//                    public void onResult(int status, String msg, AliyunSTSTokenBean bean) {
//                        stsTokenBean = bean;
//                    }
//                });
                UCommonUtil.getUtil().openVideoRadio(this, null);
                break;
            case R.id.upload:
                VideoManagerUtils.getUtils().uploadVideo(stsTokenBean, videoPath, VideoManagerUtils.getUtils().getVideoCoverPath(videoPath), new ImageManagerUtils.ImageCompressListener() {
                    @Override
                    public void onResult(int stutas, String cover, String result) {
                        if (stutas ==0){
                            UCommonUtil.getUtil().showToast(TestVideoActivity.this, "上传成功");
                        }
                    }
                });
                break;
        }
    }
}
